package 队列.基础;

//链表实现队列

class ListNode{
    int val;
    ListNode next;
    ListNode prev;

    public ListNode(int val) {
        this.val = val;
    }
}
public class MyQueue {
    private ListNode front;
    private ListNode rear;
    private int usedSize = 0;

    //插入操作 采用头插法
    public void offer(int val){
        ListNode cur = new ListNode(val);
        if(front ==null){
            front = rear = cur;
        }else {
            cur.next= front;
            cur.prev = null;
            front.prev = cur;
            front = cur;
        }
        usedSize++;
    }

    //删除操作 因为是头插法 删除的是尾巴节点
    public int poll(){

        //为空
        if(front ==null){
            return -1;
        }
        int ret = rear.val;
        //只有一个
        if(front ==rear){
            front = null;
            rear = null;
            usedSize--;
            return ret;
        }
        rear = rear.prev;
        rear.next = null;
        usedSize--;
        return ret;
    }

    public int peak(){
        if(front ==null){
            return -1;
        }
        return rear.val;
    }

    public int getUsedSize(){
        return usedSize;
    }
    public boolean isEmpty(){
        if(usedSize ==0){
            return true;
        }
        return false;
    }
}
